package com.rightsidetech.system.config;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.rightsidetech.commom.enums.SMSTemplate;
import com.rightsidetech.commom.exception.BizException;
import com.rightsidetech.commom.utils.SmsApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;

/**
 * @author yinj
 * @date 2021-4-19 17:27:18
 */
@Component
@Slf4j
public class AliyunServer {

    @Autowired
    private AliyunConfig aliyunConfig;

    public void sendMsg(String phoneNumbers, SMSTemplate smsTemplate, String ... placeHolderValues) throws Exception{
        String content = smsTemplate.getContent();
        String templateParam = "";
        if(StrUtil.isNotBlank(content)){
            //填充占位
            templateParam = fillPlaceholderValues(content, placeHolderValues);
        }

        //发送验证码
        SendSmsResponse sendSmsResponse = SmsApi.aliyunSendMsg(aliyunConfig.getAccessKeyId(), aliyunConfig.getAccessKeySecret(), aliyunConfig.getSignName(), phoneNumbers, smsTemplate.getTemplateId(), templateParam);
        //判断结果
        if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            //发送成功
        } else {
            if(sendSmsResponse.getMessage() != null){
                throw new BizException(sendSmsResponse.getMessage());
            }
            throw new BizException("短信发送返回码为空");
        }
    }

//    public Map<String, String> idenAuth(String idNo, String name) throws Exception{
//        String method = "POST";
//        Map<String, String> headers = new HashMap<>();
//        headers.put("Authorization", "APPCODE " + aliyunConfig.getAppcode());
//        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
//        Map<String, String> querys = new HashMap<>();
//        Map<String, String> bodys = new HashMap<>();
//        bodys.put("idNo", idNo);
//        bodys.put("name", name);
//
//        HttpResponse response = HttpUtils.doPost(aliyunConfig.getIdenAuthHost(), aliyunConfig.getIdenAuthPath(), method, headers, querys, bodys);
//        String result = EntityUtils.toString(response.getEntity());
//        // 解析成Map对象
//        Map<String, String> mapType = JSON.parseObject(result,Map.class);
//        String respCode = mapType.get("respCode");
//        if(respCode != null){
//            if(respCode.equals("0000")){
//               //成功
//            }else if(respCode.equals("0001")){
//                throw new BizException("姓名不能为空");
//            }else if(respCode.equals("0002")){
//                throw new BizException("姓名不能包括特殊字符");
//            }else if(respCode.equals("0003")){
//                throw new BizException("身份证号不能为空");
//            }else if(respCode.equals("0004")){
//                throw new BizException("身份证号格式错误");
//            }else if(respCode.equals("0007")){
//                throw new BizException("无此身份证号码");
//            }else if(respCode.equals("0008")){
//                throw new BizException("身份证信息不匹配");
//            }else if(respCode.equals("0010")){
//                throw new BizException("系统维护，请稍后再试");
//            }
//        } else{
//            throw new BizException("身份认证返回码为空");
//        }
//        return mapType;
//    }

    private String fillPlaceholderValues(String content, String ... placeHolderValues) throws Exception{
        String[] contentArr = content.split(",");
        if(placeHolderValues.length != contentArr.length){
            throw new BizException("短信模板占位符数量与填充值数量不符");
        }
        MapBuilder<String, String> mapBuilder = MapBuilder.create(new HashMap<>());
        for (int i = 0; i < contentArr.length; i++) {
            mapBuilder.put(contentArr[i], placeHolderValues[i]);
        }
        return JSON.toJSONString(mapBuilder.build());
    }



}
